package org.jing.ext.example;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.jing.core.logger.JingLogger;
import org.jing.ext.jdbc.JDBCBuilder;
import org.jing.ext.jdbc.mybatis.MyBatis;
import org.jing.ext.jdbc.mybatis.param.DatetimeTypeHandler;
import org.jing.ext.jdbc.mybatis.param.Params;
import org.jing.ext.jdbc.mybatis.test.domain.UserDomain;

import java.util.Properties;

/**
 * Description: <br>
 *
 * @author bks <br>
 * @since 2021-08-17 <br>
 */
public class Example4MyBatis {
    private static final JingLogger LOGGER = JingLogger.getLogger(Example4MyBatis.class);

    Example4MyBatis() throws Exception {
        Properties mainP = new Properties();
        mainP.setProperty("jdbc.driver", "org.sqlite.JDBC");
        mainP.setProperty("jdbc.url", "jdbc:sqlite:temp/test.db");

        MyBatis.setBuilder(new JDBCBuilder<SqlSessionFactory>() {
            @Override public SqlSessionFactory build() throws Throwable {
                return new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml"), mainP);
            }
        });
        Params.enableGlobalTypeHandler(DatetimeTypeHandler.class);
        try (MyBatis myBatis = MyBatis.get()) {
            UserDomain domain = myBatis.getMayBatisDomain(UserDomain.class);
            domain.enableTable();
            // domain.createUser("bks", "xxx", "asdasd@qq.com", "155****9349");
            System.out.println(domain.queryUserById(1));
        }
        LOGGER.debug("over");
    }

    public static void main(String[] args) throws Exception {
        new Example4MyBatis();
    }
}
